package com.sjm.learn.jdk8;

import java.util.Arrays;
import java.util.List;

public class Java8Demo1 {

	public static void main(String[] args) {
		final Car car = Car.create( Car::new );
		final List< Car > cars = Arrays.asList( car );
		
		cars.forEach( Car::collide );
		
		cars.forEach( Car::repair );
		
		final Car police = Car.create( Car::new );
		cars.forEach( police::follow );
		
		System.out.println("");
	}

}

@FunctionalInterface //主要用于编译级错误检查，加上该注解，当你写的接口不符合函数式接口定义的时候，编译器会报错
interface Supplier<T> {
    T get();
}
 
class Car {
    //Supplier是jdk1.8的接口，这里和lamda一起使用了
    public static Car create(final Supplier<Car> supplier) {
        return supplier.get();
    }
 
    public static void collide(final Car car) {
        System.out.println("Collided " + car.toString());
    }
    //没有static
    public void follow(final Car another) {
        System.out.println("Following the " + another.toString());
    }
 
    public void repair() {
        System.out.println("Repaired " + this.toString());
    }
}
